package cn.com.dcsgo.design.pattern.singleton;

import java.io.Serial;
import java.io.Serializable;

/**
 * 单例模式-静态内部类实现懒加载
 *
 * @author Dcsgo
 * @since 2025/9/22 8:19
 */

public class LazySingleton1 implements Serializable {
    private static class LazySingleton1Holder {
        private final static LazySingleton1 INSTANCE = new LazySingleton1();
    }

    /**
     * 私有化构造方法，不允许外部new实例化对象
     */
    private LazySingleton1() {
        if (LazySingleton1Holder.INSTANCE != null) {
            throw new IllegalStateException("非法创建示例对象！");
        }
    }

    public static LazySingleton1 getInstance() {
        return LazySingleton1Holder.INSTANCE;
    }

    /**
     * 反序列化时自动回调，保证返回唯一实例
     */
    @Serial
    private Object readResolve() {
        return LazySingleton1Holder.INSTANCE;
    }
}
